package com.ruoyi.web.controller.business;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.business.service.IHurtQuestionnaireScoreService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.HurtPatientDoctor;
import com.ruoyi.business.service.IHurtPatientDoctorService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 患者医师绑定Controller
 *
 * @author ruoyi
 * @date 2024-04-17
 */
@RestController
@RequestMapping("/business/patientDoctor")
public class HurtPatientDoctorController extends BaseController
{
    @Autowired
    private IHurtPatientDoctorService hurtPatientDoctorService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IHurtQuestionnaireScoreService hurtQuestionnaireScoreService;

    /**
     * 查询患者医师绑定列表
     */
    @PreAuthorize("@ss.hasPermi('business:patientDoctor:list')")
    @GetMapping("/list")
    public TableDataInfo list(HurtPatientDoctor hurtPatientDoctor)
    {
        startPage();
        List<HurtPatientDoctor> list = hurtPatientDoctorService.selectHurtPatientDoctorList(hurtPatientDoctor);
        return getDataTable(list);
    }

    /**
     * 导出患者医师绑定列表
     */
    @PreAuthorize("@ss.hasPermi('business:patientDoctor:export')")
    @Log(title = "患者医师绑定", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HurtPatientDoctor hurtPatientDoctor)
    {
        List<HurtPatientDoctor> list = hurtPatientDoctorService.selectHurtPatientDoctorList(hurtPatientDoctor);
        ExcelUtil<HurtPatientDoctor> util = new ExcelUtil<HurtPatientDoctor>(HurtPatientDoctor.class);
        util.exportExcel(response, list, "患者医师绑定数据");
    }

    /**
     * 获取患者医师绑定详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:patientDoctor:query')")
    @GetMapping(value = "/{patientId}")
    public AjaxResult getInfo(@PathVariable("patientId") Long patientId)
    {
        return success(hurtPatientDoctorService.selectHurtPatientDoctorByPatientId(patientId));
    }

    /**
     * 新增患者医师绑定
     */
    @PreAuthorize("@ss.hasPermi('business:patientDoctor:add')")
    @Log(title = "患者医师绑定", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HurtPatientDoctor hurtPatientDoctor)
    {
        return toAjax(hurtPatientDoctorService.insertHurtPatientDoctor(hurtPatientDoctor));
    }

    /**
     * 修改患者医师绑定
     */
    @PreAuthorize("@ss.hasPermi('business:patientDoctor:edit')")
    @Log(title = "患者医师绑定", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HurtPatientDoctor hurtPatientDoctor)
    {
        return toAjax(hurtPatientDoctorService.updateHurtPatientDoctor(hurtPatientDoctor));
    }

    /**
     * 删除患者医师绑定
     */
    @PreAuthorize("@ss.hasPermi('business:patientDoctor:remove')")
    @Log(title = "患者医师绑定", businessType = BusinessType.DELETE)
	@DeleteMapping("/{patientIds}")
    public AjaxResult remove(@PathVariable Long[] patientIds)
    {
        return toAjax(hurtPatientDoctorService.deleteHurtPatientDoctorByPatientIds(patientIds));
    }


    /**
     * 患者绑定医师
     * @param hurtPatientDoctor
     * @return
     */
    @PostMapping("/bindDoctor")
    public AjaxResult bindDoctor(@RequestBody HurtPatientDoctor hurtPatientDoctor){
        System.out.println("doctorId = " + hurtPatientDoctor);
        Long patientId = SecurityUtils.getUserId();
        //删除旧绑定
        hurtPatientDoctorService.deleteHurtPatientDoctorByPatientId(patientId);
        //新增绑定
        hurtPatientDoctor.setPatientId(patientId);
        hurtPatientDoctorService.insertHurtPatientDoctor(hurtPatientDoctor);
        return AjaxResult.success("绑定成功");
    }

    /**
     * 获取患者已绑定医师信息
     * @return
     */
    @GetMapping("/getBindDoctor")
    public AjaxResult getBindDoctor(){
        Long userId = SecurityUtils.getUserId();
        Map<String, Object> bindDoctor = hurtPatientDoctorService.getBindDoctor(userId);
        if (bindDoctor!=null){
            return AjaxResult.success(bindDoctor);
        }else {
            return AjaxResult.warn("患者未绑定医师");
        }
    }

    /**
     * 患者更换绑定医师
     * @param hurtPatientDoctor
     * @return
     */
    @PostMapping("/replaceBindDoctor")
    public AjaxResult replaceBindDoctor(@RequestBody HurtPatientDoctor hurtPatientDoctor){
        System.out.println("doctorId = " + hurtPatientDoctor);
        Long patientId = SecurityUtils.getUserId();
        //删除患者已绑定医师
        hurtPatientDoctorService.deleteHurtPatientDoctorByPatientId(patientId);
        //重新绑定
        hurtPatientDoctor.setPatientId(patientId);
        hurtPatientDoctorService.insertHurtPatientDoctor(hurtPatientDoctor);
        return AjaxResult.success("更改绑定成功");
    }

    /**
     * 医师获取绑定的患者列表
     * @return
     */
    @GetMapping("/getBindPatient")
    public TableDataInfo getBindPatient(){
        Long doctorId = SecurityUtils.getUserId();
        startPage();
        List<Map<String, Object>> list = hurtPatientDoctorService.getBindPatient(doctorId);
        for (Map<String, Object> map : list) {
            Long patientId = Long.valueOf(String.valueOf(map.get("patientId")));
            Map<String, Object> scoreMap = hurtQuestionnaireScoreService.getOdiAndVasByPatientId(patientId);
            if (scoreMap!=null){
                map.put("odi",scoreMap.get("odi"));
                map.put("vas",scoreMap.get("vas"));
                map.put("time",scoreMap.get("createTime"));
            }else {
                map.put("odi","");
                map.put("vas","");
                map.put("time","");
            }
        }
        return getDataTable(list);
    }

    /**
     * 医师获取患者信息
     * @return
     */
    @GetMapping("/getPatientInfo")
    public AjaxResult getUserInfo(@RequestParam Long patientId){
        System.out.println("patientId = " + patientId);
        Map<String, Object> userInfo = userService.getUserInfo(patientId);
        return AjaxResult.success(userInfo);
    }
}
